package controller

import (
	"context"
	"fmt"
	"gowork/studentGrpc/webMessage"

	//"time"

	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/mvc"
	//"github.com/micro/go-micro/client"
	//grpc "github.com/micro/go-grpc"
	"gowork/studentGrpc/webClient/util"
	// "github.com/micro/go-micro"

	//"github.com/afex/hystrix-go/hystrix"
)

// var (
// 	cli webMessage.StudentService
// 	//Cli webMessage.StudentService
// )

// func init(){

// 	// grpcService := grpc.NewService()
// 	// grpcService.Init()
// 	// cli = webMessage.NewStudentService("student_micro_service", grpcService.Client())

// 	// service := micro.NewService()
// 	// service.Init()
// 	// cli = webMessage.NewStudentService("student_micro_service", service.Client())
	
// 	cli = webMessage.NewStudentService("student_micro_service", client.DefaultClient)
// }


type StudentGrpcController struct{
    Ctx  iris.Context
}

func (c *StudentGrpcController) PostGrpcinsert() mvc.Result {
	id := c.Ctx.FormValue("id")
	name := c.Ctx.FormValue("name")
	birth := c.Ctx.FormValue("birth")
	sex := c.Ctx.FormValue("sex")
	
	var as = webMessage.AddUpStudent{Id: id,
							Name:name,
							Birth:birth,
							Sex:sex,
						}

	//var res *webMessage.State
	// //熔断
	// commandConfig :=hystrix.CommandConfig{
	// 	Timeout: 1000,
	// 	// RequestVolumeThreshold: 2,
	// 	// ErrorPercentThreshold: 50,
	// 	// SleepWindow: 5000,
	// }
	// var err error
	// hystrix.ConfigureCommand("insert", commandConfig)
	// err = hystrix.Do("insert", func() error {
	// 	res, err = util.Cli.InsertStudent(context.TODO(), &as)
	// 	return err
	// },func(e error) error{
	// 	res.Code = "400"
	// 	res.Message = "服务降级"
	// 	return nil
	// 	//return e
	// })
	// if err != nil {
	// 	return mvc.Response{
	// 		Object : err,
	// 	}
	// }

	res, _:= util.Cli.InsertStudent(context.Background(), &as)
	// if err != nil {
	// 	fmt.Println(err)
	// 	return mvc.Response{
	// 		Object : err,
	// 	}
	// }

	return mvc.Response{
        Object : res,
    }
}

func (c *StudentGrpcController) PostGrpcdelete() mvc.Result {
	name := c.Ctx.FormValue("name")

	var ds = webMessage.DelStudent{
		Name:name,
	}

	res, err := util.Cli.DeleteStudent(context.Background(), &ds)
	if err != nil {
		fmt.Println(err)
		return mvc.Response{
			Object : err,
		} 
	}

	return mvc.Response{
		Object : res,
	}
}

func (c *StudentGrpcController) PostGrpcupdate() mvc.Result {
	id := c.Ctx.FormValue("id")
	name := c.Ctx.FormValue("name")
	birth := c.Ctx.FormValue("birth")
	sex := c.Ctx.FormValue("sex")

	var as = webMessage.AddUpStudent{Id: id,
		Name:name,
		Birth:birth,
		Sex:sex,
	}

	res, err := util.Cli.UpdateStudent(context.Background(), &as)
	if err != nil {
		fmt.Println(err)
		return mvc.Response{
			Object : err,
		} 
	}

	return mvc.Response{
		Object : res,
	}
}

func (c *StudentGrpcController) PostGrpcquery() mvc.Result {

	// getService , sErr := util.etcdRegistry.GetService("student_micro_service")
	// if sErr != nil{
	// 	log.Fatal(sErr)
	// }
	// netx := selector.RoundRobin(getService)
	// note, err := netx()
	// if err != nil {
	// 	log.Fatal(note)
	// }

	// Cli = webMessage.NewStudentService(note.Address, client.DefaultClient)

	var l = webMessage.List{}
	
	//res, err := Cli.QueryStudent(context.TODO(), &l)
	res, err := util.Cli.QueryStudent(context.Background(), &l)
	if err != nil {
		fmt.Println(err)
		return mvc.Response{
			Object : err,
		} 
	}

	//time.Sleep(time.Second*3)
	
	return mvc.Response{
		Object : res,
	}
}


